package product;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;



public class MemberUpdate extends JInternalFrame  {

	Connection conn;
	ProductMain3 pm;
	PreparedStatement pstmt;
	ResultSet rs;
	ResultSetMetaData rsmd;
	MemberInput mi;
	MemberSelect ms;
	DefaultTableModel model = new DefaultTableModel();
	Vector<String> v;
	TableRowSorter<TableModel> tSoter;
	boolean tFlag = false;

	private JPanel contentPane;
	private JTextField codeIn;
	private JTextField phone;
	private JTextField mail;
	private JTextField irum;
	private JTextField address;
	private JTextField date;
	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	private JLabel label_5;
	private JLabel label_6;
	private JButton btnMod;

	private JTextField jumin;
	private JTextField payIn;
	private JLabel label_8;
	private JLabel label_9;

	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
	MemberData md;
	boolean mdf; //수정 확인

	String str1[]={"사원","대리","과장","부장"};//직급 콤보박스 배열
	String str2[]={"재경부","기획부","홍보부","개발부","마케팅부","총무부"};//부서 콤보박스 배열

	String position2="사원";//직급 콤보박스
	String team2="재경부";//부서 콤보박스



	Vector<String> colNam;	//여기서부터 전체조회
	String[] rows = {"사번","성명","직통번호","이메일","입사일","직책","부서"};
	Vector<String> RowData;
	private JButton btnDel;
	private JComboBox position;
	private JComboBox team;
	private JButton btnNewButton;

	public MemberUpdate(Connection conn, ProductMain3 p) {
		super("MemberUpdate", true, true, true, false);
		this.conn=conn;
		this.pm=p;

		v= new Vector<String>();

		v.add("No");
		v.add("Memberid");
		v.add("Pwd");
		v.add("Hiredate");
		v.add("Irum");
		v.add("Iden_number");
		v.add("team");
		v.add("Position");
		v.add("Phone");
		v.add("Email");
		v.add("Address");
		v.add("Pay");
		v.add("Working");

		model = new DefaultTableModel(v,0);


		setBounds(100, 100, 474, 228);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getCodeIn());
		contentPane.add(getPhone());
		contentPane.add(getMail());
		contentPane.add(getIrum());
		contentPane.add(getAddress());
		contentPane.add(getDate());
		contentPane.add(getLblNewLabel());
		contentPane.add(getLabel());
		contentPane.add(getLabel_1());
		contentPane.add(getLabel_2());
		contentPane.add(getLabel_3());
		contentPane.add(getLabel_4());
		contentPane.add(getLabel_5());
		contentPane.add(getLabel_6());
		contentPane.add(getBtnMod());

		contentPane.add(getJumin());
		contentPane.add(getPayIn());
		contentPane.add(getLabel_8());
		contentPane.add(getLabel_9());
		contentPane.add(getBtnDel());
		contentPane.add(getTeam());
		contentPane.add(getPosition());



	}

	public void setI(MemberInput mi) {
		this.mi = mi;
	}
	public void setU(MemberSelect ms){
		this.ms = ms;
	}


	public void Modify(){

		try{
			if(!payIn.getText().equals("")){				

				if(!team.getSelectedItem().equals("")){				

					if(!position.getSelectedItem().equals("")){				


						String c= codeIn.getText();

						String py= payIn.getText();

						String sql= "update memberdata set team=?, position=?, pay=?  where memberid=?";

						pstmt= conn.prepareStatement(sql);	
						pstmt.setString(1, team2);
						pstmt.setString(2, position2);
						pstmt.setString(3, py);
						pstmt.setString(4, c);
						pstmt.executeUpdate();
						pstmt.close();

						if(ms!=null) ms.columnadd();
						position2="사원";	
						team2="재경부";
						codeIn.setText("");
						irum.setText("");
						address.setText("");
						mail.setText("");
						phone.setText("");
						date.setText("");
						jumin.setText("");
						payIn.setText("");


						JOptionPane.showMessageDialog(null, "수정되었습니다.");

					}

				}

			}


		}catch(Exception e){
			e.printStackTrace();
			//			JOptionPane.showMessageDialog(null, "급여는 숫자만 입력해주세요.");
		}
	}



	public void Delete(){
		try{

			int yn = JOptionPane.showConfirmDialog(null, "정말로 삭제하겠습니까?");

			if( yn == JOptionPane.YES_OPTION){

				String c = codeIn.getText();

				String sql= "update memberdata set working=?  where memberid=?";
				pstmt= conn.prepareStatement(sql);
				pstmt.setString(1, "0");
				pstmt.setString(2, codeIn.getText());
				pstmt.executeUpdate();

				pstmt.close();

				JOptionPane.showMessageDialog(null, "퇴직처리 되었습니다");
	
				irum.setText("");
				address.setText("");
				mail.setText("");
				phone.setText("");
				date.setText("");
				jumin.setText("");
				payIn.setText("");
				btnMod.setEnabled(true);
				btnDel.setEnabled(true);
		


			}else{
				JOptionPane.showMessageDialog(null, "삭제를 취소하였습니다");		
			}

		}catch(Exception e){
			e.printStackTrace();
		}
	}





	public void serach_user(){
		try{
			String c = codeIn.getText();
			String sql = "select * from memberdata where memberid= ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, c);
			rs = pstmt.executeQuery();

			while (rs.next()) {

				String j = rs.getString("iden_number");
				String i = rs.getString("irum");
				String a = rs.getString("address");
				String p = rs.getString("phone");
				String e = rs.getString("email");
				String h = rs.getString("hiredate");
				String t = rs.getString("team");
				String po = rs.getString("position");
				String py = rs.getString("pay");

				position.setSelectedItem(po);
				team.setSelectedItem(t);

				irum.setText(i);
				jumin.setText(j);
				address.setText(a);
				phone.setText(p);
				mail.setText(e);
				date.setText(h);
				payIn.setText(py);

				btnMod.setEnabled(true);
				btnDel.setEnabled(true);

			}

		}catch(Exception e){
			e.printStackTrace();
		}
	}


	private JTextField getCodeIn() {
		if (codeIn == null) {
			codeIn = new JTextField();
			codeIn.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode()==KeyEvent.VK_ENTER){
						serach_user();
					}
				}
			});
			codeIn.setEnabled(false);
			codeIn.setBounds(74, 10, 155, 21);
			codeIn.setColumns(10);
		}
		return codeIn;
	}
	private JTextField getPhone() {
		if (phone == null) {
			phone = new JTextField();
			phone.setEditable(false);
			phone.setColumns(10);
			phone.setBounds(300, 10, 146, 21);
		}
		return phone;
	}
	private JTextField getMail() {
		if (mail == null) {
			mail = new JTextField();
			mail.setEditable(false);
			mail.setColumns(10);
			mail.setBounds(74, 103, 155, 21);
		}
		return mail;
	}
	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setEditable(false);
			irum.setColumns(10);
			irum.setBounds(74, 41, 155, 21);
		}
		return irum;
	}
	private JTextField getAddress() {
		if (address == null) {
			address = new JTextField();
			address.setEditable(false);
			address.setColumns(10);
			address.setBounds(300, 41, 146, 21);
		}
		return address;
	}
	private JTextField getDate() {
		if (date == null) {
			date = new JTextField();
			date.setEditable(false);
			date.setColumns(10);
			date.setBounds(300, 72, 146, 21);
		}
		return date;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("사원번호");
			lblNewLabel.setBounds(12, 13, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("연락처");
			label.setBounds(255, 13, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("이메일");
			label_1.setBounds(12, 106, 57, 15);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("부서");
			label_2.setBounds(255, 106, 57, 15);
		}
		return label_2;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("입사일");
			label_3.setBounds(255, 75, 57, 15);
		}
		return label_3;
	}
	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("성명");
			label_4.setBounds(12, 44, 57, 15);
		}
		return label_4;
	}
	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("주소");
			label_5.setBounds(255, 44, 57, 15);
		}
		return label_5;
	}
	private JLabel getLabel_6() {
		if (label_6 == null) {
			label_6 = new JLabel("직책");
			label_6.setBounds(12, 137, 57, 15);
		}
		return label_6;
	}

	private JButton getBtnMod() {
		if (btnMod == null) {
			btnMod = new JButton("수정");
			btnMod.setEnabled(true);
			btnMod.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {


					Modify();
					if(ms != null){
						ms.columnadd();
					}

				}

			});
			btnMod.setBounds(300, 131, 146, 21);

		}
		return btnMod;

	}
	private JTextField getJumin() {
		if (jumin == null) {
			jumin = new JTextField();
			jumin.setEditable(false);
			jumin.setColumns(10);
			jumin.setBounds(74, 72, 155, 21);
		}
		return jumin;
	}
	private JTextField getPayIn() {
		if (payIn == null) {
			payIn = new JTextField();
			payIn.setEnabled(true);
			payIn.setHorizontalAlignment(SwingConstants.RIGHT);
			payIn.setColumns(10);
			payIn.setBounds(74, 164, 155, 21);
		}
		return payIn;
	}
	private JLabel getLabel_8() {
		if (label_8 == null) {
			label_8 = new JLabel("주민번호");
			label_8.setBounds(12, 75, 57, 15);
		}
		return label_8;
	}
	private JLabel getLabel_9() {
		if (label_9 == null) {
			label_9 = new JLabel("일 급여");
			label_9.setBounds(12, 167, 57, 15);
		}
		return label_9;
	}
	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton("삭제");
			btnDel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					Delete();
					ms.columnadd();
				}
			});
			btnDel.setEnabled(true);
			btnDel.setBounds(300, 162, 146, 21);
		}
		return btnDel;
	}
	private JComboBox getPosition() {
		if (position == null) {
			position = new JComboBox(str1);//직책 콤보박스
			position.setBounds(74, 134, 155, 23);
			position.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent ex) {
					if(ex.getStateChange() == ItemEvent.SELECTED){
						position2=(String)(ex.getItem());
						System.out.println(position2);
					}


				}
			});
			position.setPreferredSize(new Dimension(115, 15));
		}
		return position;
	}
	private JComboBox getTeam() {
		if (team == null) {
			team = new JComboBox(str2);//부서 콤보박스
			team.setBounds(300, 103, 145, 23);
			team.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent ev) {
					if(ev.getStateChange() == ItemEvent.SELECTED){
						team2=(String)(ev.getItem());
						System.out.println(team2);
					}


				}
			});
			team.setPreferredSize(new Dimension(115, 17));
		}
		return team;
	}
	public void setData(String mid, String i, String j, String em, String po, String py, String pn, String ad, String h, String t){
		position.setSelectedItem(po);
		team.setSelectedItem(t);

		codeIn.setText(mid);
		irum.setText(i);
		jumin.setText(j);
		address.setText(ad);
		phone.setText(pn);
		mail.setText(em);
		date.setText(h);
		payIn.setText(py);

		//		btnMod.setEnabled(true);
		//		btnDel.setEnabled(true);
	}
}